/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import OBJECT.DangKiVanChuyen;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author Ngoc Anh
 */
public class DangKiVanChuyenDao {
      private Connect connect=null;
     
      public DangKiVanChuyenDao(String User ,String Pass,String port,String DataBase,String Host) {
        connect =new Connect( User , Pass, port, DataBase, Host);

     }
      
      public CallableStatement SearchDangKiVanChuyen(int madk)
      {
          Connection conn = connect.GetConnect();
          ResultSet result = null;
          CallableStatement cals = null;
          DangKiVanChuyen dk = new DangKiVanChuyen();
          try{
              cals = conn.prepareCall("{call sp_ThongTinDangKi(?)}");              
              cals.setInt(1, madk);
              result =cals.executeQuery();           
              while(result.next())
              {                
                  dk.setMaDangKiVC(result.getInt("Ma_DKi"));
                  dk.setMaKH(result.getInt("Ma_KH"));
                  dk.setMaVanChuyen(result.getInt("Ma_VanChuyen"));
                  dk.setTenGaDen(result.getString("TenGaDen"));
                  dk.setSoContainer(result.getInt("SoCotainer"));
                  dk.setTongTien(result.getFloat("TinhTrangDK"));
              }
              result.close();
              
          }catch(Exception exc)
          {
              return null;
          }
          finally{
             CloseConnect();
          }
          return cals;
      }
  
      public CallableStatement DanhSachDangKiVanChuyen1KH(int makh)
      {
            Connection conn=connect.GetConnect();
            ResultSet result=null;
            CallableStatement cals=null;
                try {
                    cals=conn.prepareCall("{call sp_DSDangKicuaKH(?)}");
                    cals.setInt(1, makh);
                    return cals;    

                } catch (SQLException ex) {
                ;

                } finally{
            // CloseConnect();
         } 

            return cals;
      }
      
            
      
      public void CloseConnect()
      {
          try
          {
          connect.Close();
          }catch(Exception e)
          {
              ;
          }
      } 
      
      public String ThemDangKiVanChuyen(DangKiVanChuyen dkvc)
      {
          String Loi="";
          Connection conn=connect.GetConnect();
          try{
                CallableStatement cals=conn.prepareCall("{call sp_DangKiVanChuyen_KH(?,?,?,?,?,?)}");
                cals.setInt(1,dkvc.getMaKH());
                cals.setInt(2, dkvc.getMaVanChuyen());
                cals.setString(3,dkvc.getTenGaDen());
                cals.setInt(4, dkvc.getSoConTainer());
                cals.setFloat(5, dkvc.getTongTien());               
                cals.registerOutParameter(6,java.sql.Types.NVARCHAR );
                cals.execute();
               // Loi = cals.getString(6);                
                cals.close();
          }catch(Exception exc)
          {
              System.out.println(Loi);
              Loi = "failed";
          }
          finally{
              CloseConnect();
          }
          return Loi;
      }
      
      public String HuyDangKiVanChuyen(int madk)
      {
            String Message="";
            Connection conn=connect.GetConnect();
                try {
                CallableStatement cals=conn.prepareCall("{call sp_HuyDangKy(?,?)}");
                cals.setInt(1, madk);
                cals.setString(2, "");
                cals.registerOutParameter(2, java.sql.Types.NVARCHAR);            
                cals.execute();
                Message=cals.getString(2);
            } catch (SQLException ex) {
                 Message="Faild";
               // System.out.println(ex.getMessage());
            }
            finally{
                CloseConnect();
            }
        return Message;
      }
      
      public String SuaDangKyVanChuyen(DangKiVanChuyen dkvc)
      {
          String Loi="";
        Connection conn=connect.GetConnect();
        try {
            CallableStatement cals=conn.prepareCall("{call sp_SuaDangKiVC(?,?,?,?,?,?)}");
                cals.setInt(1,dkvc.getMaDangKiVC());
                cals.setInt(2, dkvc.getMaVanChuyen());                        
                cals.setInt(3, dkvc.getSoConTainer());
                cals.setString(4, dkvc.getTenGaDen());  
                cals.setFloat(5, dkvc.getTongTien());
               // cals.setString(5, dkvc.getTinhTrangDK());
               cals.registerOutParameter(6, java.sql.Types.NVARCHAR);
               cals.execute();
               Loi=cals.getString(6);
                  
        } catch (SQLException ex) {
            Loi="Falied";
            
        }
       finally
        {
            CloseConnect();
        }
        return Loi;
      }
}
